package test;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TIJ674_EvenChecker implements Runnable{
	private TIJ674_IntGenerator generator;
	public TIJ674_EvenChecker(TIJ674_IntGenerator generator){
		this.generator =generator;
	}
	@Override
	public void run() {
		while(!generator.isCanceled()){
			int val = generator.next();
			if(val % 2 != 0){
				System.out.println(Thread.currentThread().getName() + " " +val + " is not EVEN");
				generator.cancel();
			}
			//Thread.yield();
		}
	}
	
	public static void test(TIJ674_IntGenerator generator, int count){
		System.out.println("Press CTRL+C to exit.");
		ExecutorService exec = Executors.newCachedThreadPool();
		for(int i = 0; i < count; i++){
			exec.submit(new TIJ674_EvenChecker(generator));
		}
		exec.shutdown();
	}
	
	public static void test(TIJ674_IntGenerator generator){
		test(generator, 100);
	}
}
